import { loginAPI } from '@/api/user'
// import { get } from 'core-js/core/dict'
import { getToken, setToken, removeToken } from '@/utils/auth'
export const TOKEN_KEY = 'token_key'
import { getProfileAPI } from '@/api/user'
const state = {
  // 优先从本地取，取不到再赋值空
  token: getToken || '',
  profile: {}
}
const mutations = {
  setToken(state, token) {
    state.token = token
    // 在本地存一份token
    setToken(token)
  },
  clearUserInfo(state) {
    // 清除token
    state.token = null
    // 清除本地token
    removeToken()
  },
  setProfile(state, profile) {
    state.profile = profile
  }
}
const actions = {
  async doLogin(ctx, { username, password }) {
    // 调用接口
    const res = await loginAPI({ username, password })
    // 提交mutation
    ctx.commit('setToken', res.data.token)
  },
  async getProfile(ctx) {
    const res = await getProfileAPI()
    ctx.commit('setProfile', res.data)
    return res.data.permissions
  }
}
const getters = {}
export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
